from bean.Constants import Constants


def is_first_letter_uppercase(field: str) -> bool:
    """
    判断字符串第一个字符是否为大写
    :param field: 需要判断的字段
    :return: 是否第一个字符为大写
    """
    if field[0].isupper():
        return True
    else:
        return False


def uppercase_first_char(filed: str) -> str:
    """
    将字段第一个字符转化为大写
    :param filed: 需要转换的字段
    :return: 第一个字符为大写的字段
    """
    if is_first_letter_uppercase(filed):
        return filed
    else:
        return filed[0].upper() + filed[1:]


def is_first_letter_lower(field: str) -> bool:
    """
    判断字符串第一个字符是否为小写
    :param field: 需要判断的字段
    :return: 是否第一个字符为小写
    """
    if field[0].islower():
        return True
    else:
        return False


def lower_first_char(filed: str) -> str:
    """
    将字段第一个字符转化为小写
    :param filed: 需要转换的字段
    :return: 第一个字符为小写的字段
    """
    if is_first_letter_lower(filed):
        return filed
    else:
        return filed[0].lower() + filed[1:]


def to_upper_case(filed: str, is_first_letter_upper: bool = False) -> str:
    """
    将蛇形命名改为驼峰命名
    :param filed: 需要转换的字段
    :param is_first_letter_upper: 是否为大驼峰
    :return: 转换后的字段
    """
    if "_" not in filed:
        if is_first_letter_upper:
            return uppercase_first_char(filed)
        return filed
    result = ""
    for i in filed.split("_"):
        if not is_first_letter_upper:
            is_first_letter_upper = True
            result += i
            continue
        result += uppercase_first_char(i)
    return result


def table_name_to_bean_name(filed: str, is_first_letter_upper: bool = False,
                            is_need_suffix_bean_param: bool = True) -> str:
    """
    表名转化为bean名
    :param filed: 转化的表名
    :param is_first_letter_upper: 是否为大驼峰
    :param is_need_suffix_bean_param: 是否加上配置的后缀
    :return:
    """
    if Constants.get_instance().IGNORE_TABLE_PREFIX:
        filed = filed[filed.find("_") + 1:]
    filed = to_upper_case(filed, is_first_letter_upper)
    if is_need_suffix_bean_param:
        filed += Constants.get_instance().SUFFIX_BEAN_PARAM
    return filed


def field_name_to_property_name(filed: str, is_first_letter_upper: bool = False) -> str:
    """
    字段名转化为属性名
    :param filed: 转化的字段
    :param is_first_letter_upper: 是否为大驼峰
    :return:
    """
    if Constants.get_instance().IGNORE_TABLE_PREFIX:
        filed = filed[filed.find("_") + 1:]
    filed = to_upper_case(filed, is_first_letter_upper)
    return filed


def camel_case_split(camel_str):
    """
    将驼峰命名法字符串拆分成单词列表。

    参数:
    camel_str (str): 驼峰命名法的字符串

    返回:
    list: 拆分后的单词列表
    """
    words = []
    current_word = []

    for char in camel_str:
        if char.isupper():
            if current_word:
                words.append(''.join(current_word))
                current_word = []
            current_word.append(char)
        else:
            current_word.append(char)

    # 处理最后一个单词
    if current_word:
        words.append(''.join(current_word))

    return words


def get_last_suffix(field: str) -> str:
    return camel_case_split(field)[-1]


def is_empty(field: str) -> bool:
    if field == "" or field is None:
        return True
    return False


if __name__ == "__main__":
    print(lower_first_char("Ffffe"))
